<template>
  <div class="app-container">
    <div class="filter-container" align="center">
      <el-form :inline="true" :model="pageInfo" class="demo-form-inline">

        <el-form-item label="书名">
          <el-input v-model="pageInfo.bookName" placeholder="书名" />
        </el-form-item>

        <el-form-item label="类型">
          <el-select v-model="category" placeholder="类型">
            <el-option
              v-for="item in options"
              :key="item.category"
              :label="item.category"
              :value="item.category">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="书籍">
<!--     v-model="pageInfo.intestate"            -->
          <el-select v-model="pageInfo.vip" placeholder="请选择书籍">
            <el-option label="普通书籍" value="1" />
            <el-option label="vip书籍" value="2" />
          </el-select>
        </el-form-item>



        <el-form-item>
          <el-button type="primary" class="filter-item" icon="el-icon-search" @click="onSubmit">查询</el-button>
        </el-form-item>


      </el-form>
    </div>
    <el-table :data="book" border style="width: 100%" stripe>
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="80">
      </el-table-column>
      <el-table-column label="图片" align="center" width="150">
        <template slot-scope="scope">
          <img :src="'/api'+scope.row.path" width="60px" height="60px">
        </template>
      </el-table-column>
      <el-table-column
        prop="bookName"
        label="书名"
        width="180"
        align="center"
      />
      <el-table-column
        prop="author"
        label="作者"
        width="170"
        align="center"
      />
      <el-table-column
        prop="introduce"
        label="简介"
        width="170"
        align="center"
      />
      <el-table-column
        prop="category"
        label="类别"
        width="100"
        align="center"
      />


      <el-table-column label="会员" align="center" :formatter="sta" width="100">
      </el-table-column>


      <el-table-column label="操作" align="center">
        <!--                          获取每一次循环时 的元素节点 (v-for) user in users 中的 user    -->
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            @click="handleEdit(scope.$index, scope.row)"-->
<!--          >编辑</el-button>-->
          <el-button size="mini" type="primary" @click="handleDetails(scope.$index, scope.row)" >查看详情</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div>
      一共有 {{total}} 本书籍
    </div>
    <!--    分页组件-->
    <el-pagination
      background
      :page-size="pageInfo.pageSize"
      :page-sizes="[5, 10, 20, 30]"
      :current-page="pageInfo.pageNum"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>


<script>
import axios from "axios";

export default {
  name: "UserMoneyView",
  data() {
    return {
      total: 0,
      pageInfo: {
        pageNum: 1,
        pageSize: 5,
        bookName:"",

      },
      book: [],
      options:[],
      category:"",
      user:{
        id:'',
        username:'',
      },

    }

  },
  // 前置执行方法在执行之前    beforeCreate()   axios.post(get)("http://localhost:8080/queryAll").then(...)
  created() { // 这是可以获取data中的数据后  再执行下面的axios
    this.getData()
  },
  methods: {
    sta(row) {
      if (row.vip == 1) {
        return '普通书籍'
      } else {
        return 'Vip书籍'
      }
    },
    // 查看详情
    handleDetails(index, row) {
      // alert(row.id)
      // 使用路由的push方法  跳转到修改书籍页面
      this.$router.push({ path: 'bookShelfViewDetails', query: { id: row.id }})
    },


    // 修改每页条数
    handleSizeChange(val) {
      this.pageInfo.pageSize = val
      this.getData()
    },
    // 修改当前页数
    handleCurrentChange(val) {
      this.pageInfo.pageNum = val
      this.getData()
    },
    // 获取数据
    getData() {
      //根据用户名查询用户信息
      var formData2 = new FormData();
      formData2.append("username", this.$store.state.user.name)
      axios({
        //根据用户名查询用户的个人信息
        url: '/api/user/queryUser',
        method: 'post',
        data: formData2,
        headers:{"Authorization":"Bearer "+this.$store.state.user.token}
      }).then(res => {
        // console.log(res.data.data)
        this.user = res.data.data
        // console.log(res.data.data.id)

        //获取所有的书籍类型
        axios({
          url: '/api/classification/queryAllCategory',
          method: 'post',
          headers:{"Authorization":"Bearer "+this.$store.state.user.token}
        }).then(res => {
          // console.log(res.data.data)
          this.options=res.data.data
        })


        var formData = new FormData();
        formData.append("category", this.category)
        for (const arr in this.pageInfo) {
          formData.append(arr, this.pageInfo[arr])
        }
        formData.append("userid", res.data.data.id)
        axios({
          //获取所有的用户的信息和他们的角色信息
          url: '/api/bookshelf/queryAllBookShelf',
          method: 'post',
          data: formData,
          headers:{"Authorization":"Bearer "+this.$store.state.user.token}
        }).then(res => {
          // console.log(res.data.data)
          // this.money=res.data.data.list
          this.book=res.data.data.list
          // console.log(this.role)
          // for (const arr in this.role) {
          //     this.role.name=this.role[arr].roleList.name
          // }
          this.total = res.data.data.total
        })

      })


    },
    // 查询
    onSubmit() {
      // alert(this.pageInfo.username)
      this.pageInfo.pageNum = 1 // 点击查询时  页码重置为1
      this.getData()
    },

    // 删除书架信息
    handleDelete(index, row) {
      // get post

      if (confirm('确定要删除吗')) {
        axios({
          url: '/api/bookshelf/delete/' + row.id,
          method: 'post',
          headers:{"Authorization":"Bearer "+this.$store.state.user.token}
        }).then(res => {
          if (res.data.code == 1) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            // 更新本页数据
            this.getData()
          } else {
            this.$message.error('删除失败')
          }
        })
      }
    }
    // this.$router.push({name:})
  }


}
</script>

<style scoped>

</style>
